Step 1: L1 part (weighted by ρ):
Step 2: L2 part (weighted by 1 − ρ):
Step 3: Multiply by λ and sum:
An unconstrained regression will happily grow enormous coefficients to fit noise. Regularisation prevents this by adding a penalty on coefficient magnitude to the cost function, buying a reduction in variance at the price of a little bias — often a very good trade.
The choice of penalty norm determines the character of the result. Ridge (L2) shrinks coefficients smoothly toward zero without ever quite reaching it. Lasso (L1) drives coefficients exactly to zero, which makes it a feature-selection method in its own right — an embedded method, selecting features as a side effect of fitting. Elastic Net blends the two. Because Lasso blurs the line between regularisation and selection, this chapter also covers the other selection techniques specific to regression: the correlation filter and p-value based selection, and where each sits in the filter / wrapper / embedded taxonomy.
Consider two candidate models that both fit the training data equally well:
| Model 1 (Simple) | Model 2 (Complex) |
|---|---|
| \( \hat{y} = 2x_3 + 1.4x_7 - 0.5x_9 + 4 \) | \( \hat{y} = 22x_1 - 103x_2 - 14x_3 + 109x_4 - 93x_5 + 203x_6 + 87x_7 - 55x_8 + 378x_9 - 25x_{10} + 8 \) |
We should prefer Model 1 — simpler, sparser, with smaller coefficients. Why are large / many coefficients problematic?
Solution: Add a regularization term to the cost function that penalizes coefficient magnitude. The result: the optimizer is forced to trade off low training error for model simplicity.
Two mathematical ways to penalize coefficient magnitude. Note: only the weights \( \theta_1, \ldots, \theta_p \) are regularized — never the bias \( \theta_0 \).
Used in Lasso Regression. Produces sparse solutions (some coefficients become exactly zero).
Used in Ridge Regression. Shrinks all coefficients towards zero (none hit exactly zero).
Model 1: \( \hat{y} = 2x_3 + 1.4x_7 - 0.5x_9 + 4 \) (weights: 2, 1.4, −0.5; bias 4, not penalized)
Model 2: weights = [22, −103, −14, 109, −93, 203, 87, −55, 378, −25]
Notice how the L2 penalty grows quadratically for Model 2. Because the penalty rises much faster than the L1 penalty as coefficients get larger, Ridge assigns Model 2 a far higher cost than Lasso does.
The total loss is the sum of (a) regression error (quality of fit) and (b) a regularization term (penalty for complexity):
The hyperparameter \( \lambda \) (lambda) controls the balance between performance and simplicity.
Substituting the L2 norm into the cost function gives Ridge regression, and substituting the L1 norm gives Lasso. The choice changes how the coefficients shrink, and this is what makes Lasso usable as a feature-selection method:
| Aspect | Ridge Regression (L2) | Lasso Regression (L1) |
|---|---|---|
| Full Cost Function | \( J = \frac{1}{2m}\sum(\hat{y}_i-y_i)^2 + \lambda \sum_{j=1}^{p-1} \theta_j^2 \) | \( J = \frac{1}{2m}\sum(\hat{y}_i-y_i)^2 + \lambda \sum_{j=1}^{p-1} |\theta_j| \) |
| Effect on coefficients | Shrinks all coefficients toward zero; none set to exactly zero | Shrinks and sparsifies: some coefficients become exactly zero |
| Feature selection | Retains all features (still uses them all in predictions) | Automatic feature selection (Embedded method!); zeroed-out features are dropped |
| Interpretability | Less interpretable (all p features remain) | More interpretable (sparse, fewer non-zero coefficients) |
| Standardization | Strongly recommended (L2 penalty is very scale-sensitive) | Recommended (if skipped, regularization is applied unevenly across features) |
Trying to make the model perform better can make it more complex, and vice versa. \( \lambda \) is the "knob" that resolves this tension:
Elastic Net combines both penalties. A hyperparameter \( \rho \in [0, 1] \) (rho) weights the L1 and L2 terms:
Library notation note: In scikit-learn the mixing parameter is called l1_ratio (α in some textbooks). We use \( \rho \) here to avoid confusion with the gradient-descent learning rate α.
Feature selection improves model performance, training speed, and interpretability by discarding irrelevant or redundant features. We focus on two regression-tailored methods.
In the chapter on feature selection we saw Chi-Square, ANOVA and other filter methods, all of which assume a categorical target. For regression with a continuous target, the Pearson Correlation Coefficient is the most common filter. It measures the linear relationship between each feature and the target.
Like other filter methods, we can either keep the top-k columns or select columns exceeding a threshold (say \( |r| > 0.3 \)).
Note: Correlation works with one-hot encoded categorical variables, but ANOVA or Mutual Information are more statistically natural choices for purely categorical features.
After training a linear regression model, we can examine the statistical significance of each coefficient via its p-value. Formally, we test the null hypothesis:
Decision rule (typical):
| Family | How it Works | Examples |
|---|---|---|
| Filter | Select features before training, using statistical tests independent of the final model | Chi-square, ANOVA, Pearson Correlation, Mutual Information |
| Wrapper | Train the model many times with different subsets to pick the best-performing subset | Forward Selection, Backward Elimination, Recursive Feature Elimination (RFE) |
| Embedded | Feature selection happens during / as a byproduct of model training | Tree-based feature importances, Lasso (this chapter!), p-value pruning |
A. What model do we recover when λ = 0 in Ridge regression?
B. What happens as λ → +∞ in Lasso regression?
Pick the more appropriate regularization method for each goal.
Scenario A: You have 500 features and suspect only 20 of them matter; your stakeholders want a short, human-readable list of "the drivers" to put in a report.
Scenario B: You have 20 carefully chosen features from domain experts; each is known to be important. You just want to dampen coefficients and avoid overfitting, without dropping any feature.
A dataset of 8 features has Pearson correlations with the target shown below:
| Feature | Correlation (r) with Target |
|---|---|
| Age | +0.04 |
| Income | +0.72 |
| Zip-code (one-hot) | −0.02 |
| Education-Years | +0.31 |
| Height | −0.08 |
| Credit Score | −0.58 |
| Shoe Size | +0.01 |
| Family Size | +0.22 |
Task: Apply the threshold \( |r| > 0.3 \). Which features are kept?
Caution: Correlation only captures linear association. A strong non-linear relationship could have r ≈ 0 and would be dropped by this filter.
A student argues: "Since feature X's p-value is 0.08 (greater than 0.05), we have proven that X has no effect on the target whatsoever."
Mistake: Confusing "failure to reject \( H_0 \)" with "accepting \( H_0 \)." A high p-value is not proof of no effect.
Correct interpretation:
With p = 0.08, the observed data are not sufficiently unlikely under the null hypothesis \( \theta_j = 0 \). So we fail to reject \( H_0 \) at the α = 0.05 level. This does not mean the feature is definitely irrelevant — it might be a weak effect or the sample might be too small to detect it. Use domain knowledge and cross-validated performance before dropping it.
Weights: \( \theta = [\theta_1 = 3,\ \theta_2 = -4]^T \). λ = 0.1, ρ = 0.6.
Step 1: L1 part (weighted by ρ):
Step 2: L2 part (weighted by 1 − ρ):
Step 3: Multiply by λ and sum:
Adding L2 regularization to the Normal Equation changes the closed-form solution to:
where \( I' \) is the identity matrix but with \( I'_{00} = 0 \) (bias is not regularized). Prove / reason: "Why does adding \( \lambda I' \) guarantee invertibility, even when \( X^T X \) is singular?"
Step 1: \( X^T X \) is always positive semi-definite: for any vector v, \( v^T X^T X v = \|Xv\|^2 \ge 0 \).
Step 2: Singularity ⟺ some non-zero v exists with \( \|Xv\| = 0 \) (i.e., X has linearly dependent columns).
Step 3: Adding \( \lambda I' \) (with λ > 0 and the bias trick) to \( X^T X \) shifts every eigenvalue of the feature-submatrix by λ. The result is positive definite:
Feature A: Pearson r = +0.02 with target, p-value = 0.01 after regression.
Feature B: Pearson r = +0.55 with target, p-value = 0.20 after regression.
Step 1: Feature A (r = 0.02, p = 0.01)
Step 2: Feature B (r = 0.55, p = 0.20)
Takeaway: Correlation and p-values answer different questions. Use both, not either one in isolation.
Two models with the same 3 non-bias weights: θ = [5, 0, −5].
Elastic Net penalty with λ = 0.5.
Match each description to the correct family: (F) Filter, (W) Wrapper, (E) Embedded.
Answer all 5 questions. Click an option for instant feedback.
Your score: 0 / 5